package com.fei.springfilter.config;

import com.fei.springfilter.me.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;

/***
 * @Date: 2019/9/30 12:58
 * @author: 8非
 * Description: 
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    @Autowired
    MyInterceptor myInterceptor;

    /**
     * 拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        ArrayList<String> urlList = new ArrayList<>();
        urlList.add("/test/**");
        urlList.add("/user/**");
        registry.addInterceptor(myInterceptor).addPathPatterns(urlList);
    }

    /**
     * 跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") //添加映射路径
                .allowedOrigins("*") //放行哪些原始域
                .allowedMethods("GET","POST", "PUT", "DELETE") //放行哪些原始域(请求方式)
                .allowCredentials(true)         //是否发送Cookie信息
                .allowedHeaders("*")  //放行哪些原始域(头部信息)
                .exposedHeaders("Header1", "Header2"); //暴露哪些头部信息（因为跨域访问默认不能获取全部头部信息）
    }
}
